﻿
using System;
using System.Collections.Generic;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Core.Views;
using Microsoft.Practices.Unity;


namespace Core.Presenters
{
    public class BuildingsPresenter 
    {
        private IBuildingDAO _buildingDao;
        private List<IBuilding> _buildings = new List<IBuilding>();
        private IAbstractDataView _view;
        public BuildingsPresenter(IAbstractDataView view)
        {
            _view = view;
            _buildingDao = ResolverContainer.Resolver.Resolve<IBuildingDAO>();
            view.AddRecord += AddRecord;
            view.Refreh += Refreh;
            view.DeleteRecord += DeletRecord;
            view.UpdateRecord += Update;
            _buildings = _buildingDao.GetAll(true);
            view.Data.DataSource = _buildings;
            view.Data.ResetBindings(true);
            view.ViewName = "Просмотр зданий";
            view.Show();

        }

        private void AddRecord(object sender,EventArgs eventArgs )
        {
            var building = new BuildingPresenter(new BuildingView());
            Refreh(this, null);
        }

        private void DeletRecord(object sender, EventArgs eventArgs)
        {
            if (_view.Data.Current != null)
            {
                var current = _view.Data.Current;
                _buildingDao.Delete((IDomainObject) current);
                Refreh(this,null);
            }
        }

        private void Refreh(object sender, EventArgs eventArgs )
        {
            _buildings = _buildingDao.GetAll(false);
            _view.Data.DataSource = _buildings;
            _view.Data.ResetBindings(true);
        }


        private void Update(object  sender, EventArgs eventArgs)
        {
            if (_view.Data.Current != null)
            {
                var current = _view.Data.Current;
                var upd = new BuildingPresenter(new BuildingView(), (IBuilding) current);
                Refreh(this, null);
            }
        }
        
    }
}
